//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - S+S Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeUI;
using LitwareHR.Infrastructure.Interface;
using Microsoft.Practices.CompositeUI.EventBroker;
using LitwareHR.PublicApp.WinUX.Services;
using Microsoft.Practices.CompositeUI.Utility;
using LitwareHR.PublicApp.WinUX.Constants;
using System.Drawing;
using System.Windows.Media.Imaging;
using LitwareHR.Infrastructure.Interface.Constants;
using System.Windows;
using System.Windows.Interop;

namespace LitwareHR.PublicApp.WinUX
{
    public partial class HeaderViewPresenter : Presenter<IHeaderView>
    {
        TenantBrandingService _tenantBrandingService = null;

        public HeaderViewPresenter([ServiceDependency] TenantBrandingService tenantBrandingService )
        {
            this._tenantBrandingService = tenantBrandingService;
        }

        [EventSubscription(EventTopicNames.NewLogoProgress, ThreadOption.UserInterface)]
        public void OnNewLogoProgress(object sender, DataEventArgs<int> progress)
        {
            View.ShowProgress(progress.Data);         
        }

        [EventSubscription(EventTopicNames.NewLogo, ThreadOption.UserInterface)]
        public void OnNewLogo(object sender, DataEventArgs<Bitmap> image )
        {
            BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(
                   image.Data.GetHbitmap(),
                   IntPtr.Zero,
                   Int32Rect.Empty,
                   System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

            image.Data.Dispose();
            View.SetImage(bitmapSource);
        }

        /// <summary>
        /// This method is a placeholder that will be called by the view when it has been loaded.
        /// </summary>
        public override void OnViewReady()
        {
            base.OnViewReady();
            View.SetAnimationDefaults();
            _tenantBrandingService.GetTenantLogo();
        }

        /// <summary>
        /// Close the view
        /// </summary>
        public void OnCloseView()
        {
            base.CloseView();
            WorkItem.SmartParts.Remove(View);
            if (View is IDisposable)
                ((IDisposable)View).Dispose();
        }

        [EventSubscription(EventTopicNames.Connect, ThreadOption.UserInterface)]
        public void OnConnect(object sender, EventArgs eventArgs)
        {
            View.ShowConnectAnimation();
        }

        [EventSubscription(EventTopicNames.Disconnect, ThreadOption.UserInterface)]
        public void OnDisconnect(object sender, EventArgs eventArgs)
        {
            View.ShowDisconnectAnimation();
        }
    }
}

